package com.atguigu.gmall.payment.service.impl;

import com.atguigu.gmall.payment.mapper.PaymentInfoMapper;
import com.atguigu.gmall.payment.model.PaymentInfo;
import com.atguigu.gmall.payment.service.PaymentInfoService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

/**
 * @author: atguigu
 * @create: 2023-08-12 15:17
 */
@Service
public class PaymentInfoServiceImpl extends ServiceImpl<PaymentInfoMapper, PaymentInfo> implements PaymentInfoService {

    /**
     * 保存本地交易记录信息
     *
     *
     * @param paymentInfo 保证入参对象相关属性都赋值完成
     * @param paymentType
     */
    @Override
    public void savePaymentInfo(PaymentInfo paymentInfo, String paymentType) {
        //1.根据订单编号+支付方式查询本地交易记录
        LambdaQueryWrapper<PaymentInfo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(PaymentInfo::getOutTradeNo, paymentInfo.getOutTradeNo());
        queryWrapper.eq(PaymentInfo::getPaymentType, paymentType);
        PaymentInfo paymentInfoDB = this.getOne(queryWrapper);

        //2.如果本地交易记录存在，直接返回接口
        if (paymentInfoDB != null) {
            return;
        }
        //3.如果本地交易记录不存在，新增本地交易记录即可
        this.save(paymentInfo);
    }

    /**
     * 根据订单编号+支付方式查询本地交易记录
     * @param outTradeNo
     * @param payType
     * @return
     */
    @Override
    public PaymentInfo getPaymentInfo(String outTradeNo, String payType) {
        LambdaQueryWrapper<PaymentInfo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(PaymentInfo::getOutTradeNo, outTradeNo);
        queryWrapper.eq(PaymentInfo::getPaymentType, payType);
        PaymentInfo paymentInfoDB = this.getOne(queryWrapper);
        return paymentInfoDB;
    }
}
